//
//  BaseVistorController.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/19.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class BaseVistorController: UIViewController
{

    //判断有没有登录
    //根据ViewModel中的登录信息来设置登录信息
    var isLogin:Bool = AuthViewModel.AuthViewModelTools.isLogin
    //var isLogin:Bool = false
    
    
    //懒加载一个tableView
    lazy var tableView:UITableView = UITableView()
    
    var vistorView:VistorView?
    
    
    
    //通过有没有登录来加载不同的视图
    override func loadView()
    {
        //当已经登录时，加载tableView
        isLogin ? (view = tableView) : (setUpVistorView())
    }
    
    func setUpVistorView()
    {
        //加载另一种访客view
        self.vistorView = VistorView()
        
        //为防止出现循环引用，需要在self前加入weak
        self.vistorView?.LoginRegClosure = {[weak self] in
            
            self?.EnterToLoginVC()
            
        }
        
        view = vistorView
        
        //设置navigationItem的button
        setUpNavigationItem()
        
    }
    
     //设置navigationItem的button
    func setUpNavigationItem()
    {
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登录", imageStr: nil, target: self, action: #selector(BaseVistorController.BarButtonClick))
        
        self.navigationItem.rightBarButtonItem = NewBarButtonItem(title: "注册", imageStr: nil, target: self, action: #selector(BaseVistorController.BarButtonClick))
    }
    
    
    func BarButtonClick()
    {
        //print("点击了BarButtonItem登录或注册按钮")
        self.EnterToLoginVC()
    }
    
    //利用modal的方式跳转到登录/注册控制器
    func EnterToLoginVC()
    {
        let LoginNav = UINavigationController(rootViewController: AuthViewController())
        
        presentViewController(LoginNav, animated: true, completion: nil)
        
    }
    
    
    /*
    deinit
    {
        print("当前VC销毁了")
    }
    */
    
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
    
    

}
